package com.edooteach.api.web.controller.shopping;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.demo2do.core.entity.result.HttpResult;
import com.edoomath.entity.course.Course;
import com.edoomath.entity.mall.Sku;
import com.edooteach.api.servcice.CourseService;
import com.edooteach.api.servcice.SkuService;

@Controller
@RequestMapping("/shopping-sku")
public class SkuApiController {

    private static final Logger logger = LoggerFactory.getLogger(SkuApiController.class);

    @Autowired
    private SkuService skuService;

    @Autowired
    private CourseService bookService;
    
    /**
     * list sKu, list sku's books
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult list() {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            List<Sku> skus = skuService.listBookSku();
            httpResult.success().data("skus", skus);
            
        } catch (Exception e) {
            logger.error("ActivityController#onCreate() error:", e);
            httpResult.fail("500").message("获取所有书籍失败");
            
        }
        
        return httpResult;
    }
    
    /**
     * load sKu, list sku's books
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult load(@PathVariable("id") Long id) {

        HttpResult httpResult = new HttpResult();

        try {
            Sku sku = skuService.load(Sku.class, id);

            List<Course> courses = bookService.listBySku(sku);
            httpResult.success().data("courses", courses).data("sku", sku).message("获取所有书籍成功");

        } catch (Exception e) {
            logger.error("ActivityController#onCreate() error:", e);
            httpResult.fail("500").message("获取所有书籍失败");

        }

        return httpResult;
    }

}
